import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class SendFile {
	Socket socket = null;
	ServerSocket server = null;
	
	public SendFile(ServerSocket ss, Socket s){
		socket = s;
	}
	
	public void makeDialog(){
		JFrame frame = new JFrame();
		JFileChooser choose = new JFileChooser();
		javax.swing.filechooser.FileFilter filter = new javax.swing.filechooser.FileFilter(){

			@Override
			public boolean accept(File f) {
				if(f.isDirectory()) return true;
				if(f.getName().toLowerCase().endsWith(".txt")) return true;
				if(f.getName().toLowerCase().endsWith(".pdf")) return true;
				if(f.getName().toLowerCase().endsWith(".doc")) return true;
				if(f.getName().toLowerCase().endsWith(".docx")) return true;
				if(f.getName().toLowerCase().endsWith(".xls")) return true;
				if(f.getName().toLowerCase().endsWith(".xlsx")) return true;
				if(f.getName().toLowerCase().endsWith(".avi")) return true;
				if(f.getName().toLowerCase().endsWith(".mp3")) return true;
				if(f.getName().toLowerCase().endsWith(".mp4")) return true;
				if(f.getName().toLowerCase().endsWith(".m4a")) return true;
				if(f.getName().toLowerCase().endsWith(".mkv")) return true;
				if(f.getName().toLowerCase().endsWith(".flv")) return true;
				if(f.getName().toLowerCase().endsWith(".chm")) return true;
				if(f.getName().toLowerCase().endsWith(".jar")) return true;
				return false;
			}

			@Override
			public String getDescription() {
				// TODO Auto-generated method stub
				return null;
				}
			};
			choose.addChoosableFileFilter(filter);
			choose.setVisible(true);
			int value = choose.showOpenDialog(choose);
			if(value == JFileChooser.CANCEL_OPTION)
				choose.setVisible(false);
			if(value == JFileChooser.APPROVE_OPTION){
				File file = choose.getSelectedFile();
				String absol = file.getAbsolutePath();
				String rel = this.getFilename(absol);
				try {
					DataOutputStream os = new DataOutputStream(socket.getOutputStream());
					if(socket != null){
						JOptionPane.showMessageDialog(frame, rel);
						os.writeBytes(rel);
						os.close();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				/*
				try {
					FileInputStream stream = new FileInputStream(file);
					OutputStream out = socket.getOutputStream();
					byte[] buffer = new byte[socket.getSendBufferSize()];
					int bytes = stream.read(buffer);
					while(bytes > 0){
						out.write(buffer,0,bytes);
						bytes = stream.read(buffer);
					}
					stream.close();
					socket.close();
					out.close();
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				JOptionPane.showMessageDialog(frame, "File sent successfully");
				*/
			}
		}
	
		public String getFilename(String s){
			int index = s.length();
			String filename = null;
			for(int i = s.length(); i >= 3; i--){
				if(s.charAt(i) == '\\'){
						index = i;
				}
			}
			filename = s.substring(index+1, s.length());
			return filename;
		}
	}